{
 "cells": [
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "On Balance Volume: 能量潮\n",
    "动能， 应该由成交量的变化情况来反应。 通过量入手， 对价格走势做出预测。\n",
    "分为：\n",
    "累积OBV\n",
    "  上涨进行 正累加   ； 下跌进行  负累加\n",
    "  OBV(n) = +-V(n) + OBV(n-1)\n",
    "  \n",
    "移动OBV\n",
    "\n",
    "修正型OBV\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "import os\n",
    "import talib as ta\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "path = 'F:\\HQData\\market_A\\\\'\n",
    "\n",
    "tsingTaoFile = path + '600600.csv'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>High</th>\n",
       "      <th>Open</th>\n",
       "      <th>Low</th>\n",
       "      <th>Close</th>\n",
       "      <th>preclose</th>\n",
       "      <th>Volume</th>\n",
       "      <th>curvalue</th>\n",
       "      <th>signType</th>\n",
       "      <th>dkWarnType</th>\n",
       "      <th>bonusInfo</th>\n",
       "      <th>bonusInfoType</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Date</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2021-10-22</th>\n",
       "      <td>79.50</td>\n",
       "      <td>78.40</td>\n",
       "      <td>77.52</td>\n",
       "      <td>79.47</td>\n",
       "      <td>78.39</td>\n",
       "      <td>3151166.0</td>\n",
       "      <td>247030076.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-10-21</th>\n",
       "      <td>79.64</td>\n",
       "      <td>79.32</td>\n",
       "      <td>77.76</td>\n",
       "      <td>78.39</td>\n",
       "      <td>79.32</td>\n",
       "      <td>5997256.0</td>\n",
       "      <td>470661996.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-10-20</th>\n",
       "      <td>81.37</td>\n",
       "      <td>81.00</td>\n",
       "      <td>79.14</td>\n",
       "      <td>79.32</td>\n",
       "      <td>80.76</td>\n",
       "      <td>5122720.0</td>\n",
       "      <td>407732583.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-10-19</th>\n",
       "      <td>81.12</td>\n",
       "      <td>80.30</td>\n",
       "      <td>79.79</td>\n",
       "      <td>80.76</td>\n",
       "      <td>80.30</td>\n",
       "      <td>5029041.0</td>\n",
       "      <td>403918892.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-10-18</th>\n",
       "      <td>83.96</td>\n",
       "      <td>83.58</td>\n",
       "      <td>79.75</td>\n",
       "      <td>80.30</td>\n",
       "      <td>84.10</td>\n",
       "      <td>7611919.0</td>\n",
       "      <td>613102168.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "             High   Open    Low  Close  preclose     Volume     curvalue  \\\n",
       "Date                                                                       \n",
       "2021-10-22  79.50  78.40  77.52  79.47     78.39  3151166.0  247030076.0   \n",
       "2021-10-21  79.64  79.32  77.76  78.39     79.32  5997256.0  470661996.0   \n",
       "2021-10-20  81.37  81.00  79.14  79.32     80.76  5122720.0  407732583.0   \n",
       "2021-10-19  81.12  80.30  79.79  80.76     80.30  5029041.0  403918892.0   \n",
       "2021-10-18  83.96  83.58  79.75  80.30     84.10  7611919.0  613102168.0   \n",
       "\n",
       "            signType  dkWarnType bonusInfo  bonusInfoType  \n",
       "Date                                                       \n",
       "2021-10-22         0           0       NaN              0  \n",
       "2021-10-21         0           0       NaN              0  \n",
       "2021-10-20         0           0       NaN              0  \n",
       "2021-10-19         0           0       NaN              0  \n",
       "2021-10-18         0           0       NaN              0  "
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\n",
    "file = tsingTaoFile\n",
    "if not os.path.exists(file):\n",
    "    print('do not exist')\n",
    "else:\n",
    "    df = pd.read_csv(file)\n",
    "    df.rename( columns={'times':'Date','openp':'Open', 'highp':'High','lowp':'Low',\"nowv\":'Close','curvol':\"Volume\"}, inplace=True)\n",
    "    df.set_index('Date', inplace=True)\n",
    "    df.index = pd.to_datetime(df.index)\n",
    "\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Date\n",
       "2021-08-10    14343404.0\n",
       "2020-06-24     5436199.0\n",
       "2019-08-05      850589.0\n",
       "2018-08-03    -5888089.0\n",
       "2017-07-28    -7503036.0\n",
       "Name: OBV, dtype: float64"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_tsingtao = df\n",
    "df_tsingtao.Volume = df_tsingtao.Volume.replace(0, np.nan)\n",
    "df_tsingtao = df_tsingtao.dropna()\n",
    "close = df_tsingtao.Close\n",
    "volumn = df_tsingtao.Volume\n",
    "\n",
    "#计算OBV\n",
    "difclose = close.diff()\n",
    "difclose[0] = 0\n",
    "\n",
    "OBV = (((difclose >= 0) * 2 - 1) * volumn).cumsum()\n",
    "OBV.name='OBV'\n",
    "OBV.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "count    2.500000e+01\n",
       "mean    -9.686499e+06\n",
       "std      7.261016e+06\n",
       "min     -1.962798e+07\n",
       "25%     -1.288028e+07\n",
       "50%     -1.194778e+07\n",
       "75%     -7.582792e+06\n",
       "max      1.434340e+07\n",
       "Name: OBV, dtype: float64"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "OBV.describe()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 计算移动OBV\n",
    "smOBV = ta.SMA(OBV, 9)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 修正OBV\n",
    "# 多空比率净额 （Volume Accumulation）\n",
    "VA(n) = VA(n-1) + V(n) * [(C - L) - (H - C)]/(H-L)\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
